package HousingService.task2.serialization;

import HousingService.task1.actor.Renter;
import HousingService.task2.serialization.deserializator.AbstractReader;
import HousingService.task2.serialization.factory.JSNFactory;
import HousingService.task2.serialization.serializator.AbstractWriter;

import java.io.*;


/**
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 30.11.2010
 * Time: 17:46:11
 */
public class SerDemo {
    public static void main(String[] args) {
        JSNFactory factory = new JSNFactory();
        AbstractWriter jsnWriter = factory.createWriter();
        AbstractReader jsnReader = factory.createReader();
        


        //writes-reads Renters
        jsnWriter.erase(jsnWriter.getRentersOut());
        for(short i = 0; i < 10; i++){
           jsnWriter.write(new Renter());
        }

        //displayFile(jsnWriter.getRentersOut());
        for(Renter renter : jsnReader.readRenter(jsnWriter.getRentersOut())) {
            System.out.println(renter);
        }

        
        /*//writes Address
        jsnWriter.erase(jsnWriter.getAddressOut());
        for(short i = 0; i < 10; i++) {
            jsnWriter.write(new Address());
        }

        //displayFile(jsnWriter.getAddressOut());
        System.out.println("Addresses:");
        for(Address addr : jsnReader.readAddress(jsnWriter.getAddressOut())) {
            System.out.println(addr);
        }*/




    }//main()
    
    private static void displayFile(File file) {
        FileReader reader = null;
        BufferedReader in = null; 

        try {
            reader =  new FileReader(file);
            in = new BufferedReader(reader);
            String line;
            while((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();  
                }
            }
        }

    }
}
